<template>
	<view class="tabs">
		<view class="top box">
			<view class="iconfont iconfanhui flex-1" @tap="back">
			</view>
			<view class="title flex-1">
				消息通知
			</view>
			<view class="icon flex-1 box d-ai-c box">
				<view class="box d-ai-c flex-3 address-box d-jc-end" @tap="issue" v-if="userInfo.userInfo.roles.indexOf('teacher')>-1 || userInfo.userInfo.roles.indexOf('teacher_leader')>-1">
					我发布的
				</view>
			</view>
		</view>
		<view v-if="isNull" class="cart_null">
			<image src="../../static/images/xiaoxi@2x.png" mode=""></image>
			<text>暂时还没有通知消息~</text>
		</view>
		<scroll-view v-if="!isNull" :refresher-triggered=refresherTriggered :refresherThreshold=refresherThreshold
				@refresherrefresh='refresherrefresh()' refresher-background="#FFFFFF"  @scrolltolower='nextPage()'
				 
				 lower-threshold="500" class="" scroll-y :style="{height: orderHeight+'px'}" >
			<view class="scrollAll">
				<view class="content" v-for="(item,index) in MessageToList" :key="index" @tap="jumpMesInfo(item.id,item.role)">
					<view class="content_l">
						<view class="t-top">
							<image class="t-image" :src="item.fromUserImage" mode=""></image>
							<view class="t-name">
								<text>{{item.fromUserName}}</text>
								<text>{{item.role===1?'(教务)':'(老师)'}}</text>
							</view>
							
						</view>
						<view class="t-time">
							{{item.createTime}}
						</view>
					</view>
					<view class="content_m">
						{{item.title}}
					</view>
					<view class="content_n">
						{{item.content}}
					</view>
					<view class="content_o">
						<view class="">
							{{item.fromShopName}}
						</view>
						<view class="" v-if="item.status===0">
							待查看
						</view>
						<view class="" v-if="item.status===1" style="color:#999999">
							已读
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		
		data() {
			return {
				isNull: false,
				orderHeight: '',
				MessageToList:'',
				pageNum:1,
				pageSize:10,
				hasNextPage: true,
				refresherThreshold: 50,
				refresherTriggered: false,
				_refresherTriggered: false,
			}
		},
		computed: mapState(['loginState','userInfo']),
		
		onReady() {
			let that = this;
			uni.getSystemInfo({ //调用uni-app接口获取屏幕高度
				success(res) { //成功回调函数
					that.orderHeight = res.windowHeight - uni.upx2px(165)
					//windoHeight为窗口高度，主要使用的是这个
				}
			})
		},
		onLoad(){
			if(this.loginState){
				this.getMessageToList()
			}else{
				uni.navigateTo({
					url:'../user/login'
				})
			}
		},
		onShow(){
			if(this.loginState){
				this.getMessageToList()
			}else{
				uni.navigateTo({
					url:'../user/login'
				})
			}
		   
		},
		methods: {
			refresherrefresh() {
				console.log("自定义下拉刷新被触发");
				let _this = this;
				if (_this._refresherTriggered) {
					return;
				}
				_this._refresherTriggered = true;
				//界面下拉触发，triggered可能不是true，要设为true
				if (!_this.refresherTriggered) {
					_this.refresherTriggered = true;
				}
				// _this.getClassOrdersList(_this.payState)
				setTimeout(() => {
					_this.refresherTriggered = false; //触发onRestore，并关闭刷新图标
					_this._refresherTriggered = false;
				}, 1500)
			
			},
			nextPage() {
				console.log("====出发底部===")
				console.log("====出发底部===" + this.pageNum)
				console.log("====出发底部===" + this.hasNextPage)
				if (this.hasNextPage) {
					this.pageNum = this.pageNum + 1
					console.log(this.pageNum)
					this.$api.getMessageToList({
					},this.pageNum, this.pageSize).then((res) => {
						if (res.data.code == 200) {
							console.log(res.data)
							// this.MessageToList = res.data.dataMap
							var arr = res.data.dataMap.list
							// for (var i = 0; i < arr.length/2; i++) {
							// var temp = arr[i]; //交换变量
							// arr[i] = arr[arr.length - i - 1];
							// arr[arr.length-i-1]=temp;
							// }
							// console.log(arr)
							// this.MessageToList = arr
							for (var i = 0; i < arr.length; i++) {
								this.MessageToList.push(arr[i])
							}
							this.hasNextPage = res.data.dataMap.hasNextPage
						}
					})
				}
				
			
			},
			//跳转详情
			jumpMesInfo(id,role){
				uni.navigateTo({
					url: './messageInfo?id='+id+'&role='+role,
					animationType: 'pop-in',
					animationDuration: 200
				})
			},
			//获取消息通知列表
			getMessageToList(){
				this.pageNum = 1
				this.$api.getMessageToList({					
				},this.pageNum, this.pageSize).then((res) => {
					if (res.data.code == 200) {
						console.log(res.data)
						// this.MessageToList = res.data.dataMap
						var arr = res.data.dataMap.list
						// for (var i = 0; i < arr.length/2; i++) {
						// var temp = arr[i]; //交换变量
						// arr[i] = arr[arr.length - i - 1];
						// arr[arr.length-i-1]=temp;
						// }
						console.log(arr)
						this.MessageToList = arr
						this.hasNextPage = res.data.dataMap.hasNextPage
						this.isNull = this.MessageToList.length == 0 ? true : false
					}
				})
			},
			back(){
				uni.navigateTo({
					url: '../index/index',
					animationType: 'pop-in',
					animationDuration: 200
				})
			},
			issue() {
				console.log(1)
				uni.navigateTo({
					url: './myissue',
					animationType: 'pop-in',
					animationDuration: 200
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.cart_null {
		display: flex;
		width: 100%;
		flex-direction: column;
		background-color: #FFFFFF;
	}
	
	.cart_null text {
		margin: 0 auto;
		color: #666666;
	}
	
	.cart_null image {
		margin: 60rpx auto;
		width: 250rpx;
		height: 250rpx;
	}
	.scrollAll {
		overflow: hidden;
	}

	.tabs {
		flex: 1;
		flex-direction: column;
		background-color: #F8F8F8;
		overflow: hidden;
	}

	.top {
		color: #fff;
		padding: var(--status-bar-height) 20rpx 20rpx 20rpx;
		background-color: rgb(255, 85, 81);

		.iconfanhui {
			font-size: 32rpx;
		}

		.title {
			text-align: center;
			font-size: 32rpx;
		}
	}

	page {
		background-color: #F8F8F8;
	}

	.content {
		width: 690rpx;
		padding-bottom: 18rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: 30rpx auto;
		overflow: hidden;
	}

	.content_l {
		width: 630rpx;
		height: 48rpx;
		margin: 0 auto;
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.t-top {
		display: flex;
	}

	.t-image {
		width: 48rpx;
		height: 48rpx;
		border-radius: 50%;
	}

	.t-name {
		font-size: 28rpx;
		font-weight: 500;
		color: #333333;
		margin-left: 21rpx;
	}

	.t-time {
		font-size: 22rpx;
		font-weight: 500;
		color: #666666;
	}

	.content_m {
		width: 630rpx;
		height: 70rpx;
		line-height: 70rpx;
		margin: 0 auto;
		// border-bottom: 2rpx solid #EEEEEE;
		font-size: 32rpx;
		font-weight: 500;
		color: #333333;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}

	.content_n {
		width: 630rpx;
		margin: 0 auto;
		line-height: 36rpx;
		margin-top: 0rpx;
		font-size: 30rpx;
		// font-weight: 500;
		color: #666666;  
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
	}

	.content_o {
		width: 630rpx;
		margin: 0 auto;
		margin-top: 13rpx;
		font-weight: 500;
		color: #999999;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.content_o>view:nth-child(1){
		font-size: 22rpx;
	}
	.content_o>view:nth-child(2){
		color: #FF5551;
		font-size: 22rpx;
	}
</style>
